package com.hoperun.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoperun.bean.AdminManager;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.query.Criteria;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminManagerSV;
import com.hoperun.utils.ErrorCode;
import com.hoperun.utils.LoginHelper;

/**
 * Copyright: Copyright (c) 2016 GUO~AN <br/>
 * ClassName: AdminManagerController.java
 * 
 * @Description:
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2016-1-8 上午9:11:06
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2016-1-8
 *        fuyu v1.0.0 修改理由
 */
@Controller
@RequestMapping("/admin/admin/manager")
@RequiresUser
public class AdminManagerController extends BaseController {

	@Resource
	private IAdminManagerSV adminManagerSV;

	@ModelAttribute
	public void populateModel(final HttpServletRequest request,
			ModelMap modelMap) {
		try {
			super.initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/{menu}/{menuNext}")
	public String index(@PathVariable long menu, @PathVariable long menuNext,
			ModelMap modelMap) {

		modelMap.put("menuType", menu);
		modelMap.put("menuNextType", menuNext);

		return "admin:adminmanager.list";
	}

	@RequestMapping("/page")
	@ResponseBody
	public BootGridPage<AdminManager> identityUserPage(
			BootGrid<AdminManager> grid, AdminManager adminManager,
			ModelMap modelMap) {
		BootGridPage<AdminManager> pageModel = new BootGridPage<AdminManager>();

		try {
			Criteria criteria = new Criteria();

			if (null == grid)
				return new BootGridPage<AdminManager>();

			Long loginId = LoginHelper.getLoginUserId() ;
			
			criteria.addNotEqual(AdminManager.COL_MANAGER_ID, loginId, Criteria.AND) ;
			
			pageModel = adminManagerSV.queryPage(criteria, grid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageModel;
	}

	@RequestMapping("/create")
	public String create(ModelMap modelMap) {
		modelMap.addAttribute("method", "save");
		return "adminAlone:adminmanager.info";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AdminManager adminManager = null;
		try {
			adminManager = adminManagerSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("method", "update");
		modelMap.addAttribute("row", adminManager);
		return "adminAlone:adminmanager.info";
	}

	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable Long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AdminManager adminManager = null;
		try {
			adminManager = adminManagerSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("detail", adminManager);
		return "adminAlone:adminmanager.detail";
	}

	

	@RequestMapping("/save")
	@ResponseBody
	public DataView save(AdminManager adminManager) {
		DataView dataView = new DataView();

		try {
			adminManagerSV.save(adminManager);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("新增成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/update")
	@ResponseBody
	public DataView update(AdminManager adminManager) {
		DataView dataView = new DataView();

		if (null == adminManager
				|| StringUtils.isBlank("" + adminManager.getManagerId())) {
			throw new XHZException(ErrorCode.MISS_PARAM, null,
					new Object[] { "id信息" });
		}

		try {
			adminManagerSV.update(adminManager);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/find/{id}")
	@ResponseBody
	public AdminManager findOne(@PathVariable Long id) {
		AdminManager adminManager = null;
		try {
			adminManager = adminManagerSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return adminManager;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public DataView delete(Long[] ids) {
		DataView dataView = new DataView();
		AdminManager adminManager = null;
		try {
			if (ArrayUtils.isEmpty(ids)) {
				throw new XHZException(ErrorCode.ILLEGAL_PARAM, null,
						new Object[] { "id" });
			}
			for (Long id : ids) {
				adminManager = new AdminManager();
				adminManager.setManagerId(id);
				// adminManagerSV.update(adminManager);

			}
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dataView;
	}
}
